//
//  YWCoreImageView.m
//  YWPhotoEditMaster
//
//  Created by jun peng on 2023/8/20.
//

#import "YWCoreImageView.h"

@interface YWCoreImageView ()

//原始view
@property(nonatomic,strong)UIImageView *bgImageView;

/** 要绘制的图片 */
@property (nonatomic, strong) UIImage * ogrinalImage;

@property(nonatomic,assign)CGFloat brightness1;//亮度
@property(nonatomic,assign)CGFloat saturability1;//饱和度
@property(nonatomic,assign)CGFloat contrast1;//对比度
@end

@implementation YWCoreImageView


-(UIImageView *)bgImageView{
    if(_bgImageView == nil){
        UIImageView *bgImageView = [[UIImageView alloc] init];
        _bgImageView = bgImageView;
    }
    return _bgImageView;
}

//纯代码用这个
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

//xib用这个
-(void)awakeFromNib{
    [super awakeFromNib];
    [self setup];
}

//初始化
-(void)setup{
    [self addSubview:self.bgImageView];
    self.clipsToBounds = YES;
    
    self.saturability1 = 1;
    self.brightness1 = 0;
    self.contrast1 = 1;
    
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    self.bgImageView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
}


//设置初始图片
- (void)setFirstImage:(UIImage *)image{
    
    _ogrinalImage = image;
    
    self.bgImageView.image = image;
}

-(void)setBrightness:(CGFloat)brightness{
    self.brightness1 = brightness;
    
    self.bgImageView.image = [self getNewImage];
    
}
-(void)setSaturability:(CGFloat)saturability{
    self.saturability1 = saturability;
    
    self.bgImageView.image = [self getNewImage];
    
}
-(void)setContrast:(CGFloat)contrast{
    self.contrast1 = contrast;
    
    self.bgImageView.image = [self getNewImage];
    
}

//这个调整图片通过CIFilter做的
//参考网址：https://www.likecs.com/show-307897441.html
-(UIImage *)getNewImage{
    CIImage *beginImage = [CIImage imageWithCGImage:self.ogrinalImage.CGImage];
    CIFilter * filter = [CIFilter filterWithName:@"CIColorControls"];
    [filter setValue:beginImage forKey:kCIInputImageKey];
    //  饱和度 1     0---2
    [filter setValue:[NSNumber numberWithFloat:self.saturability1] forKey:@"inputSaturation"];
    //  亮度  0   -1---1
    [filter setValue:[NSNumber numberWithFloat:self.brightness1] forKey:@"inputBrightness"];
    //  对照度 1  0---4
    [filter setValue:[NSNumber numberWithFloat:self.contrast1] forKey:@"inputContrast"];
    
    // 得到过滤后的图片
    CIImage *outputImage = [filter outputImage];
    // 转换图片, 创建基于GPU的CIContext对象
    CIContext *context = [CIContext contextWithOptions: nil];
    CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]];
    UIImage *newImg = [UIImage imageWithCGImage:cgimg];
    
    // 释放C对象
    CGImageRelease(cgimg);
    
    return newImg;
    
    
}



//获取当前图层
-(UIImage *)getCurrentLayer{
    //1.开启一个位图上下文
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0);
    
    //2.把画板上的内容渲染到上下文当中
    CGContextRef ctx =  UIGraphicsGetCurrentContext();
    [self.layer renderInContext:ctx];
    
    //3.从上下文当中取出一张图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    //4.关闭上下文
    UIGraphicsEndImageContext();
    
    return newImage;
    
}


@end
